package com.tjsinfo.tjsoa.resource.duty.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;

import com.tjsinfo.tjsoa.resource.duty.vo.TResourceDuty;


/**
 * A data access object (DAO) providing persistence and search support for
 * TResourceDuty entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.tjsinfo.tjsoa.resource.duty.TResourceDuty
 * @author MyEclipse Persistence Tools
 */

public class TResourceDutyDao extends HibernateDaoSupport implements ITResourceDutyDao {
	
	private static final Log log = LogFactory.getLog(TResourceDutyDao.class);
	public int save(TResourceDuty transientInstance) {
		log.debug("saving TResourceDuty instance");
		int id=0;
		try {
			getHibernateTemplate().save(transientInstance);
			id=transientInstance.getId();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		return id;
	}

	public void delete(TResourceDuty transientInstance) {
		log.debug("deleting TResourceDuty instance");
		try {
			getHibernateTemplate().delete(transientInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public boolean update(TResourceDuty transientInstance) {
		log.debug("saving TResourceDuty instance");
		try {
			this.getHibernateTemplate().update(transientInstance);
			log.debug("save successful");
			return true;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}	
	
	public TResourceDuty findById(java.lang.Integer id) {
		log.debug("getting TResourceDuty instance with id: " + id);
		try {
			TResourceDuty instance = (TResourceDuty) getHibernateTemplate()
			     .get("com.tjsinfo.tjsoa.resource.duty.vo.TResourceDuty", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public TResourceDuty findByDutyDate(java.lang.String dutyDate) {
		log.debug("getting TResourceDuty instance with dutyDate: " + dutyDate );
		try {
			TResourceDuty instance = (TResourceDuty) getSession().get(
					"com.tjsinfo.tjsoa.resource.duty.vo.TResourceDuty", dutyDate);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List findByExample(TResourceDuty instance) {
		log.debug("finding TResourceDuty instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TResourceDuty instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from TResourceDuty as model where model."
					+ propertyName + "= ? order by id asc";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all TResourceDuty instances");
		try {
			String queryString = "from TResourceDuty";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

//	public List findByLikeProperty(String likeName, Object value){
//		Session session=getSession();
//		log.debug("finding TResourceDuty instance with property: "
//				+ likeName + ", value: " + value);
//		try {
//			Criteria criteria = session.createCriteria(TResourceDuty.class);
//			criteria.add(Expression.like(likeName, value));
//			criteria.addOrder(Order.desc("id"));
//			return criteria.list();
//		} catch (RuntimeException re) {
//			log.error("find by property name failed", re);
//			throw re;
//		}
//	}
	public List findByLikeProperty(String likeName, Object value){
		log.debug("finding TResourceDuty instance with property: "
				+ likeName + ", value: " + value);
		try {
			String queryString = "from TResourceDuty as model where model."
					+ likeName + " like ? order by id asc";
			return this.getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

}